package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.UserPersonalizedRecommendations;
import com.example.mall.dto.UserPersonalizedRecommendationsDto;
import com.example.mall.mapper.UserPersonalizedRecommendationsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserPersonalizedRecommendationsQuery;
import com.example.mall.service.IUserPersonalizedRecommendationsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户个性化推荐表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class UserPersonalizedRecommendationsServiceImpl extends ServiceImpl<UserPersonalizedRecommendationsMapper, UserPersonalizedRecommendations> implements IUserPersonalizedRecommendationsService {

    @Autowired
    private UserPersonalizedRecommendationsMapper userPersonalizedRecommendationsMapper;

    @Override
    public PageDTO<UserPersonalizedRecommendationsDto> queryPage(UserPersonalizedRecommendationsQuery userPersonalizedRecommendationsQuery) {
        if (userPersonalizedRecommendationsQuery == null) {
            userPersonalizedRecommendationsQuery = new UserPersonalizedRecommendationsQuery();
        }
        Page<UserPersonalizedRecommendations> page = userPersonalizedRecommendationsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserPersonalizedRecommendations> cartPage = lambdaQuery()
                .like(userPersonalizedRecommendationsQuery.getRecommendId()!= null, UserPersonalizedRecommendations::getRecommendId, userPersonalizedRecommendationsQuery.getRecommendId())
                .like(userPersonalizedRecommendationsQuery.getUserId()!= null, UserPersonalizedRecommendations::getUserId, userPersonalizedRecommendationsQuery.getUserId())
                .like(userPersonalizedRecommendationsQuery.getRecommendScore()!= null, UserPersonalizedRecommendations::getRecommendScore, userPersonalizedRecommendationsQuery.getRecommendScore())
                .like(userPersonalizedRecommendationsQuery.getStatus()!= null, UserPersonalizedRecommendations::getStatus, userPersonalizedRecommendationsQuery.getStatus())
                .like(userPersonalizedRecommendationsQuery.getProductId()!= null, UserPersonalizedRecommendations::getProductId, userPersonalizedRecommendationsQuery.getProductId())
                .orderByDesc(UserPersonalizedRecommendations::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, UserPersonalizedRecommendationsDto.class);
    }

    @Override
    public void insert(UserPersonalizedRecommendationsDto userPersonalizedRecommendationsDto) {
        UserPersonalizedRecommendations userPersonalizedRecommendations = new UserPersonalizedRecommendations();
        CopyUtils.copyProperties(userPersonalizedRecommendationsDto, userPersonalizedRecommendations);
        userPersonalizedRecommendations.setRecommendId(CodeGenerateUtils.generateUniqueId());
        userPersonalizedRecommendations.setCreateDate(new Date());
        userPersonalizedRecommendations.setUpdateDate(new Date());
        userPersonalizedRecommendations.setStatus(BaseConstants.EFFECTIVE_STATUS);
        userPersonalizedRecommendationsMapper.insert(userPersonalizedRecommendations);
    }

    @Override
    public void update(UserPersonalizedRecommendationsDto userPersonalizedRecommendationsDto) {
        AssertUtil.isNotNull(userPersonalizedRecommendationsDto.getRecommendId(), "recommendId不能为空");
        UserPersonalizedRecommendations userPersonalizedRecommendations = userPersonalizedRecommendationsMapper.selectById(userPersonalizedRecommendationsDto.getRecommendId());
        AssertUtil.isNotNull(userPersonalizedRecommendations, "用户个性化推荐表不存在");
        CopyUtils.copyProperties(userPersonalizedRecommendationsDto, userPersonalizedRecommendations);
        userPersonalizedRecommendations.setUpdateDate(new Date());
        userPersonalizedRecommendationsMapper.updateById(userPersonalizedRecommendations);
    }

    @Override
    public void delete(Long recommendId) {
        AssertUtil.isNotNull(recommendId, "recommendId不能为空");
        UserPersonalizedRecommendations userPersonalizedRecommendations = userPersonalizedRecommendationsMapper.selectById(recommendId);
        AssertUtil.isNotNull(userPersonalizedRecommendations, "用户个性化推荐表不存在");
        userPersonalizedRecommendations.setStatus(BaseConstants.LOSE_STATUS);
        userPersonalizedRecommendations.setUpdateDate(new Date());
        userPersonalizedRecommendationsMapper.updateById(userPersonalizedRecommendations);
    }

    @Override
    public void deleteBatch(List<Long> recommendIds) {
        AssertUtil.isNotNull(recommendIds, "recommendIds不能为空");
        List<UserPersonalizedRecommendations> userPersonalizedRecommendations = userPersonalizedRecommendationsMapper.selectBatchIds(recommendIds);
        AssertUtil.isNotNull(userPersonalizedRecommendations, "用户个性化推荐表不存在");
        for (UserPersonalizedRecommendations userPersonalizedRecommendation : userPersonalizedRecommendations) {
            userPersonalizedRecommendation.setStatus(BaseConstants.LOSE_STATUS);
            userPersonalizedRecommendation.setUpdateDate(new Date());
            userPersonalizedRecommendationsMapper.updateById(userPersonalizedRecommendation);
        }
    }

    @Override
    public List<UserPersonalizedRecommendations> queryUserPersonalizedRecommendationsList() {
        return this.list();
    }

    @Override
    public UserPersonalizedRecommendations queryUserPersonalizedRecommendationsById(Long recommendId) {
        AssertUtil.isNotNull(recommendId, "recommendId不能为空");
        return userPersonalizedRecommendationsMapper.selectById(recommendId);
    }
}
